package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 供应商管理
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    //分页查询供应商
//    http://localhost:8080/supplier/list
    @PostMapping("/list")
    public Map<String,Object> list(Integer page, Integer rows, String supplierName){
        return supplierService.list(page,rows,supplierName);
    }

    //供应商添加或修改
    //http://localhost:8080/supplier/save?supplierId=1
    //,@RequestParam String supplierId  @RequestBody @RequestParam(value = "supplierId")
    @PostMapping("save")
    public ServiceVO saveSupplier(Supplier supplier,String supplierId){
        try {
            supplierService.saveSupplier(supplier,supplierId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //删除供应商（支持批量删除）
//    http://localhost:8080/supplier/delete
    @PostMapping("delete")
    public ServiceVO delete(String[] ids){
        try {
            supplierService.delete(ids);
            return  new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/supplier/getComboboxList
    //供应商下拉列表查询 String q（供应商名称模糊查询）
    @PostMapping("getComboboxList")
    public List<Supplier> getComboboxList(String q){
       return supplierService.getComboboxList(q);
    }

}
